<script>

import { useMenuApi, useMenuListApi, useMenuSubmitApi } from '@/api/system/menu'

export default {
  name: 'MenuEdition',
  components: { },
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        dictType: '',
        dictName: '',
        sort: 0,
        dictSource: 0,
        dictSql: '',
        remark: '',
        pid: 0
      },
      pid: null,
      update: false,
      dataRules: {
        dictType: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        dictName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        dictSource: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        dictSql: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
      },
      menuList: []
    }
  },
  methods: {
    init(id, pid) {
      this.visible = true
      this.dataForm.id = null
      this.dataForm.pid = pid
      const formInstance = this.$refs.dataFormRef
      if (formInstance) {
        formInstance.resetFields()
      }
      this.getMenu()
      if (id) {
        this.getBaseClass(id)
        this.update = true
      }
    },
    getBaseClass(id) {
      useMenuApi(id).then(res => {
        Object.assign(this.dataForm, res.data)
      })
    },
    submitHandle() {
      this.$refs.dataFormRef.validate((valid) => {
        if (!valid) {
          return false
        }
        useMenuSubmitApi(this.dataForm).then(() => {
          this.$message.success({
            message: '操作成功',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        })
      })
    },
    menuTypeChange(pid) {
      this.getMenu()
      this.dataForm.pid = pid
    },
    getMenu() {
      const awaitMenuList = async() => {
        const res = await useMenuListApi(this.dataForm.type)
        this.menuList = res.data
      }
      return awaitMenuList()
    }
  }
}
</script>

<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px">
      <el-form-item prop="type" label="类型">
        <el-radio-group v-model="dataForm.type" :disabled="update" @change="menuTypeChange(dataForm.pid)">
          <el-radio :label="0">菜单</el-radio>
          <el-radio :label="1">按钮</el-radio>
          <el-radio :label="2">接口</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="name" label="名称">
        <el-select-tree
          v-model="dataForm.pid"
          style="width: 100%"
          :props="{
            value: 'id',
            label: (data) => data.name,
            children: 'children',
          }"
          :data="menuList"
          placeholder="请选择父级菜单"
        />
      </el-form-item>
      <el-form-item prop="name" label="名称">
        <el-input v-model="dataForm.name" placeholder="名称" />
      </el-form-item>
      <el-form-item v-if="dataForm.type === 0" prop="url" label="路由">
        <el-input v-model="dataForm.url" placeholder="路由" />
      </el-form-item>
      <el-form-item prop="sort" label="排序">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序" />
      </el-form-item>
      <el-form-item v-if="dataForm.type === 0" prop="openStyle" label="打开方式">
        <el-radio-group v-model="dataForm.openStyle">
          <el-radio :label="0">内部打开</el-radio>
          <el-radio :label="1">外部打开</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="authority" label="授权标识">
        <el-input v-model="dataForm.authority" placeholder="多个用逗号分隔，如：sys:menu:save,sys:menu:update" />
      </el-form-item>
      <el-form-item v-if="dataForm.type === 0" prop="icon" label="图标" class="popover-list">
        <el-input v-model="dataForm.icon" :readonly="true" placeholder="图标" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
